package com.sys.service.impl;

import com.common.encryt.MD5;
import com.common.exception.BusinessException;
import com.common.util.FtpUtils;
import com.sys.mapper.CommonFileMapper;
import com.sys.model.CommonFile;
import com.sys.service.CommonFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * Created by dell on 2016/6/7.
 */
@Service
public class CommonFileServiceImpl implements CommonFileService {

    @Autowired
    private CommonFileMapper commonFileMapper;
    @Override
    public int insert(CommonFile role) {
        return commonFileMapper.insert(role);
    }

    @Override
    public int delete(String id) {
        return commonFileMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(CommonFile role) {
        return commonFileMapper.updateByPrimaryKey(role);
    }

    @Override
    public CommonFile selectById(String id) {
        return commonFileMapper.selectByPrimaryKey(id);
    }

    @Override
    public CommonFile selectByCode(String code) {
        CommonFile commonFile=new CommonFile();
        commonFile.setFileCode(code);
        return commonFileMapper.selectOne(commonFile);
    }

    @Override
    public CommonFile upLoad(MultipartFile file, String path) throws IOException, NoSuchAlgorithmException {
        byte[] bytes=file.getBytes();

        CommonFile commonFile=selectByCode(MD5.MD5Encode(MD5.byteArrayToHexString(bytes)));
        if(commonFile!=null){

        }else {
            commonFile=new CommonFile();
            commonFile.setFileCode(MD5.MD5Encode(MD5.byteArrayToHexString(bytes)));
            commonFile.setFileName(file.getOriginalFilename());
            commonFile.setFileType(file.getContentType());
            commonFile.setFileSize(file.getSize());
            commonFile.setId(UUID.randomUUID().toString());
            commonFile.setFilePath(commonFile.getId()+commonFile.getFileName().substring(commonFile.getFileName().indexOf(".")));
//            String path=multipartResolver.getFileItemFactory().getRepository().getPath();
            File pathFile=new File(path);
            if(!pathFile.exists()){
                pathFile.mkdirs();
            }
            file.transferTo(new File(path+"/"+commonFile.getFilePath()));
            insert(commonFile);
        }
        return commonFile;
    }

    @Override
    @Transactional
    public CommonFile upLoadFtp(MultipartFile file, String path) throws IOException, NoSuchAlgorithmException {
        byte[] bytes=file.getBytes();
        CommonFile commonFile=selectByCode(MD5.MD5Encode(MD5.byteArrayToHexString(bytes)));
        if(commonFile!=null){
        }else {
            commonFile=new CommonFile();
            commonFile.setFileCode(MD5.MD5Encode(MD5.byteArrayToHexString(bytes)));
            commonFile.setFileName(file.getOriginalFilename());
            commonFile.setFileType(file.getContentType());
            commonFile.setFileSize(file.getSize());
            commonFile.setId(UUID.randomUUID().toString());
            commonFile.setFilePath(commonFile.getId()+commonFile.getFileName().substring(commonFile.getFileName().lastIndexOf(".")));
            FtpUtils ftpUtils= FtpUtils.getInstanse();
            boolean isSuccess = ftpUtils.uploadFile(path,commonFile.getFilePath(),file.getInputStream());
            if(isSuccess) insert(commonFile);
        }
        return commonFile;
    }
}
